<?php

namespace Modules\User\Jobs;

use App\Jobs\Job;

use Storage;

class ReplaceHeadimg extends Job 
{
    protected $user;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($user)
    {
        $this->user = $user;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $user = $this->user;

        $disk = Storage::disk();
        $file = file_get_contents($user->headimg);

        $dir = '/img/' . str_random(20);
        $disk->put($dir, $file);
        $user->headimg = $disk->getUrl($dir);
        
        $user->save();
    }
}
